package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketStorage;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.filter.AuthenticationInfoFilter;
import com.cskaoyan.service.AdminStorageService;
import com.cskaoyan.service.AdminStorageServiceimpl;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.ResponseUtil;
import com.cskaoyan.util.StringUtil;
import com.cskaoyan.util.URIUtil;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author Ramsey
 * @date 2023年05月29日 9:00
 */
@MultipartConfig
@WebServlet("/admin/storage/*")
public class AdminStorageServlet extends WdBaseServlet {
    AdminStorageService storageService = new AdminStorageServiceimpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        AuthenticationInfoFilter authenticationInfoFilter = new AuthenticationInfoFilter();

        String operation = URIUtil.getOperation(this.getClass(), req);
        switch (operation) {
            case "/list":
                list(req, resp);
                break;
            case "/update":
                update(req, resp);
                break;
            case "/delete":
                delete(req, resp);
                break;
            case "/create":
                create(req, resp);
                break;

        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);

    }

    @SneakyThrows
    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        String jsonStr = req.getReader().readLine();
        MarketStorage storage = JacksonUtil.read(jsonStr, MarketStorage.class);
        BaseRespVo delete = storageService.delete(storage);
        ResponseUtil.responseJson(resp, delete);
    }

    @SneakyThrows
    private void update(HttpServletRequest req, HttpServletResponse resp) {
        String jsonStr = req.getReader().readLine();
        MarketStorage storage = JacksonUtil.read(jsonStr, MarketStorage.class);
        BaseRespVo update = storageService.update(storage);
        ResponseUtil.responseJson(resp, update);
    }

    @SneakyThrows
    private void list(HttpServletRequest req, HttpServletResponse resp) {



        String key = StringUtil.isEmpty(req.getParameter("key")) ? null : req.getParameter("key");
        //新增根据name查询
        String name = StringUtil.isEmpty(req.getParameter("name")) ? null : req.getParameter("name");


        Integer page = StringUtil.isNotEmpty(req.getParameter("page")) ? Integer.parseInt(req.getParameter("page")) : null;
        Integer limit = StringUtil.isNotEmpty(req.getParameter("limit")) ? Integer.parseInt(req.getParameter("limit")) : null;
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        List<MarketStorage> storageList = storageService.list(key, page, limit, sort, order,name);
        //        ResponseUtil.responseJson(resp, BaseRespVo.ok(CommonData.data(data)));
        ResponseUtil.responseOkJsonVo(resp, CommonData.data(storageList));
    }

    //上面是新增的2023年6月1日21:51:20
    protected void create(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进入图片上传功能");
        Part filePart = request.getPart("file");
        InputStream inputStream = filePart.getInputStream();
        String originFileName = filePart.getSubmittedFileName();
        MarketStorage marketStorage = storageService.store(inputStream, filePart.getSize(),
                filePart.getContentType(), originFileName);
        ResponseUtil.responseOkJsonVo(response, marketStorage);
    }
}
